﻿@namespace BlazorCharts
@inherits ElementChart<TData>
@typeparam TData

@if (Chart.IsInit && Chart.BcLegend == this)
{
    <g class="bc-legend" transform="translate(@Rect.LT.ToPoint())">
        @if (BorderWidth > 0)
        {
            <rect x="0" y="0" width="@Rect.W" height="@Rect.H" fill="none" stroke="@(BorderColor ?? "#C0C0C0FF")" stroke-width="@BorderWidth" />
        }

        @for (int i = 0; i < Chart.BcSeriesGroup.GroupNames.Count; i++)
        {

            //计算图例项目的基础位置
            var yBase = 0;
            var xBase = 0;
            if (_ItemDirection == "Row")
            {
                xBase = Padding.L + _ItemSize.W * i;
                yBase = Padding.T;
            }
            else
            {
                xBase = Padding.L;
                yBase = Padding.T + _ItemSize.H * i;
            }

            //颜色和文字坐标
            var xIcon = xBase + ItemSpacing / 2 + BorderWidth;
            var yIcon = yBase + ItemSpacing / 2 + BorderWidth;

            var xText = xIcon + FontSize * 1.5;
            var yText = yBase + FontSize + ItemSpacing / 2 + BorderWidth;

            var groupName = Chart.BcSeriesGroup.GroupNames[i];
            var color = groupName.RandomColor().ToHtml();

            <rect x="@xIcon" y="@yIcon" width="@FontSize" height="@FontSize" fill="@color" />
            <text>
                <text x="@xText" y="@yText" text-anchor="@TextAnchor.Start.ToDescription()" font-size="@FontSize">@groupName</text>
            </text>
        }
    </g>
}

